package zqtools;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.annotation.write.style.*;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.enums.poi.HorizontalAlignmentEnum;
import com.alibaba.excel.enums.poi.VerticalAlignmentEnum;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.util.StringUtils;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

import zqdb.O_INFO;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.time.OffsetDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class Test {
    public static void main(String[] args) throws Exception {
        String s1 = "08:15";
        String s2 = "08:19";
        Date d = new Date();
        SimpleDateFormat sf2 = new SimpleDateFormat("HH:mm01555");
        String ls_time = sf2.format(d);
        System.out.println(ls_time);
        //System.out.println(s2.compareTo(s1));


///////////////////////////////////////////////////////////////////////////////////////////////////////
//        String str = "2023-10-12 15:16:17";
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//        Date d = sdf.parse(str);
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(d);
//        System.out.println(calendar.get(Calendar.SECOND));

///////////////////////////////////////////////////////////////////////////////////////////////////////
//        int year = calendar.get(Calendar.YEAR);
//        int month = calendar.get(Calendar.MONTH);
//        int day = calendar.get(Calendar.DAY_OF_MONTH);
//        int hour = calendar.get(Calendar.HOUR_OF_DAY);
//        int minute = calendar.get(Calendar.MINUTE);
//        System.out.println(year);

        //calendar.set(year, month, day, hour, minute + 3);


//////////////////////////////////////////////////////////////////////////////////////////////////////////
//        String str = "123456789000";
//        System.out.println(str.substring(0, str.length() -3));




//        System.out.println(f_MD5("{\n" +
//                "        \"machineid\":\"12345678\",\n" +
//                "        \"machinecode\":\"0228\"\n" +
//                "    }"));

//        O_INFO of = new O_INFO();
//        of.setCMD("aaaa");
//        of.setVALUE01("1111111111111111111");
//        of.setVALUE02("2222222222222222222");

//        XStream xStream = new XStream(new DomDriver());
//        String ls_xml = xStream.toXML(of);
//        ls_xml = "<?xml version=\"1.0\" encoding=\"gb2312\"?>\n" + ls_xml;
//
//        System.out.println(ls_xml);

//        O_INFO of = new O_INFO();
//        of.setCMD("CG");
//
//        XStream xStream = new XStream(new DomDriver());
//        //设置T_FORM类的别名
//        xStream.alias("sysinfo", O_INFO.class);
//        //将Java对象序列化成XML
//        String ls_xml = xStream.toXML(of);
//        ls_xml = "<?xml version=\"1.0\" encoding=\"gb2312\"?>\n" + ls_xml;
//        String ls_sql = "{call WP_PC2(?)}";
//        Connection conn = DBUtil.getConn();
//        CallableStatement callsm = null;
//        try {
//            callsm = conn.prepareCall(ls_sql);
//            //给存储过程传参
//            callsm.setString(1, ls_xml);
//            //执行存储过程
//            callsm.execute();
//
//            int fl = 1;
//            //获取结果集
//            ResultSet rs1 = callsm.getResultSet();
//            int columnCount = callsm.getResultSet().getMetaData().getColumnCount();
//            HSSFWorkbook workbook = new HSSFWorkbook();                        // 创建工作簿对象
//            String title = "消费流水表";
//            HSSFSheet sheet = workbook.createSheet(title);                     // 创建工作表
//
//            List<HashMap<String, Object>> listMap = new ArrayList<>();
//            while (rs1.next()) {
//                HashMap<String, Object> dataMap = new HashMap<>();
//                for (int i = 0; i < columnCount; i++) {
//                    dataMap.put("VALUE" + String.format("%02d", i + 1), rs1.getString(i + 1));
//                }
//                listMap.add(dataMap);
//            }
//
//            String[] rowsName = new String[]{"工号", "姓名", "部门", "交易时间", "期初金额", "消费金额",
//                    "账户结余", "消费比率", "刷卡形式", "类型", "终端", "商户", "类别"};
//            List<Object[]> dataList = new ArrayList<>();
//            Object[] objs;
//            for (HashMap<String, Object> data : listMap) {
//                objs = new Object[rowsName.length];
//                for (int k = 0; k < columnCount; k++) {
//                    objs[k] = data.get("VALUE" + String.format("%02d", k + 1));
//                }
//
//                dataList.add(objs);
//            }
//
//            ExportExcel ex = new ExportExcel(title, rowsName, dataList);
//            ex.export(workbook, sheet);
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                callsm.close();
//                conn.close();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }


//        simpleWrite();

//        setExcelRespProp(response, "会员列表");
//        List<Member> memberList = LocalJsonUtil.getListFromJson("json/members.json", Member.class);
//        EasyExcel.write(response.getOutputStream())
//                .head(Member.class)
//                .excelType(ExcelTypeEnum.XLSX)
//                .sheet("会员列表")
//                .doWrite(memberList);


//        SimpleDateFormat sdf = new SimpleDateFormat("HH");
//        String ls_creattime = sdf.format(new Date());
//        System.out.println(ls_creattime);
//
//        Date date = new Date();
//        System.out.println(date.getTime());

//        String ls_bb, ls_ee;
//        ls_bb = "220507";
//        ls_ee = "220501";

//        System.out.println(ls_bb.compareTo(ls_ee));

//        截取右边的10位字符
//        String ls_no = "8";
//        ls_no = "00000000000" + ls_no;
//        ls_no = ls_no.substring(ls_no.length()-10);
//        System.out.println(ls_no);
    }

    public static void simpleWrite() {
        String fileName = "d:\\abc.xlsx";
        // 分页查询数据
        EasyExcel.write(fileName, DemoData.class)
                .sheet("会员列表")
                .doWrite(Test::data);
    }

    public static String f_MD5(String data) throws Exception {
        java.security.MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] array = md.digest(data.getBytes(StandardCharsets.UTF_8));
        StringBuilder sb = new StringBuilder();
        for (byte item : array) {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100), 1, 3);
        }
        return sb.toString();
    }

    private static List<DemoData> data() {

        O_INFO of = new O_INFO();
        of.setCMD("AN");
        of.setVALUE01("0");
        of.setVALUE02("100");
//        of.setVALUE03("2022-11-01 00:00:00");
//        of.setVALUE04("2022-12-31 23:59:59");

        of.setVALUE03("2022-11-28 00:00:00");
        of.setVALUE04("2022-12-31 23:59:59");

        of.setVALUE05("0");
        of.setVALUE06("全部");
        of.setVALUE07("全部");
        of.setVALUE08("全部");
        of.setVALUE09("QUERY");

        XStream xStream = new XStream(new DomDriver());
        //设置T_FORM类的别名
        xStream.alias("sysinfo", O_INFO.class);
        //将Java对象序列化成XML
        String ls_xml = xStream.toXML(of);
        ls_xml = "<?xml version=\"1.0\" encoding=\"gb2312\"?>\n" + ls_xml;
        String ls_sql = "{call WP_PC2(?)}";
        Connection conn = DBUtil.getConn();
        CallableStatement callsm = null;
        List<DemoData> list = ListUtils.newArrayList();
        try {
            callsm = conn.prepareCall(ls_sql);
            //给存储过程传参
            callsm.setString(1, ls_xml);
            //执行存储过程
            callsm.execute();

            //获取结果集
            ResultSet rs1 = callsm.getResultSet();
            while (rs1.next()) {
                DemoData data = new DemoData();
                data.setGh(rs1.getString("JOB_NO"));
                data.setXm(rs1.getString("L_NAME"));
                data.setBm(rs1.getString("L_BY4"));
                data.setJysj(rs1.getString("M_TIME"));
                data.setQcje(Double.valueOf(rs1.getString("L_JE1")));
                data.setXfje(Double.valueOf(rs1.getString("L_JE5")));
                data.setZhjy(Double.valueOf(rs1.getString("L_JE7")));
                data.setXfbl(Double.valueOf(rs1.getString("L_JE8")));
                data.setSkxs(rs1.getString("L_BY2"));
                data.setLx(rs1.getString("B_JUD"));
                data.setZd(rs1.getString("L_JQLS"));
                data.setSh(rs1.getString("L_BY1"));
                data.setLb(rs1.getString("Z_IDD"));

                list.add(data);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                callsm.close();
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return list;
    }
}

@Getter
@Setter
@ContentRowHeight(22)
@ContentFontStyle(fontHeightInPoints = 12)
@HeadStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER)//表头样式,默认就是居中
@EqualsAndHashCode
class DemoData {
    @ExcelProperty("工号")
    @ColumnWidth(12)
    @ContentStyle(horizontalAlignment= HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)//内容样式
    private String gh;

    @ExcelProperty("姓名")
    @ColumnWidth(10)
    @ContentStyle(horizontalAlignment= HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)//内容样式
    private String xm;

    @ExcelProperty("部门")
    @ColumnWidth(38)
    @ContentStyle(verticalAlignment = VerticalAlignmentEnum.CENTER)//内容样式
    private String bm;

    @ExcelProperty("交易时间")
    @ColumnWidth(22)
    @ContentStyle(horizontalAlignment= HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)//内容样式
    private String jysj;

    @ExcelProperty("期初金额")
    @ColumnWidth(15)
    @ContentStyle(verticalAlignment = VerticalAlignmentEnum.CENTER)//内容样式
    private Double qcje;

    @ExcelProperty("消费金额")
    @ColumnWidth(15)
    @ContentStyle(verticalAlignment = VerticalAlignmentEnum.CENTER)//内容样式
    private Double xfje;

    @ExcelProperty("账户结余")
    @ColumnWidth(15)
    @ContentStyle(verticalAlignment = VerticalAlignmentEnum.CENTER)//内容样式
    private Double zhjy;

    @ExcelProperty("消费比率")
    @ColumnWidth(12)
    @ContentStyle(horizontalAlignment= HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)//内容样式
    private Double xfbl;

    @ExcelProperty("刷卡形式")
    @ColumnWidth(13)
    @ContentStyle(horizontalAlignment= HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)//内容样式
    private String skxs;

    @ExcelProperty("类型")
    @ColumnWidth(15)
    @ContentStyle(horizontalAlignment= HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)//内容样式
    private String lx;

    @ExcelProperty("终端")
    @ColumnWidth(20)
    @ContentStyle(horizontalAlignment= HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)//内容样式
    private String zd;

    @ExcelProperty("商户")
    @ColumnWidth(8)
    @ContentStyle(horizontalAlignment= HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)//内容样式
    private String sh;

    @ExcelProperty("类别")
    @ColumnWidth(8)
    @ContentStyle(horizontalAlignment= HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)//内容样式
    private String lb;

    /**
     * 忽略这个字段
     */
    @ExcelIgnore
    private String ignore;
}